const { app, BrowserWindow, Menu, MenuItem, ipcMain } = require('electron');
const server = require('./server');
const { join } = require('path');
const menu = new Menu();
var serverApp = null;
function createWindow() {
  let mainWindow = new BrowserWindow({
    width: 1300,
    height: 700,
    minWidth: 1000,
    minHeight: 600,
    webPreferences: {
      nodeIntegration: true,
      contextIsolation: false,
      preload: join(__dirname, 'preload.js')
    },
    // icon: join(__dirname, 'favicon.ico'), // icon
  })
  if (app.isPackaged) mainWindow.loadFile('./dist/index.html');
  else mainWindow.loadURL('http://127.0.0.1:8080/');
  menu.append(new MenuItem({
    // label: 'tools',
    accelerator: 'CmdOrCtrl+shift+i',
    click: () => { mainWindow.webContents.openDevTools(); }
  }))
  mainWindow.setMenu(menu);
}
app.whenReady().then(() => {
  // Menu.setApplicationMenu(null);
  createWindow();
  app.on('activate', function () {
    if (BrowserWindow.getAllWindows().length === 0) createWindow()
  });

})
ipcMain.on("startServer", (event, data) => {
  serverApp = server(data);
  event.sender.send('startServer', '运行成功')
})
ipcMain.on("stopServer", (event) => {
  serverApp.close();
  console.log(serverApp.close);
  event.sender.send('stopServer', '已结束')
})
ipcMain.on("resetServer", (event, data) => {
  serverApp.close();
  serverApp = server(data);
  event.sender.send('resetServer', '重启完成')
})
app.on('window-all-closed', function () {
  if (process.platform !== 'darwin') app.quit()
})